package smartbooks.ui.manager;

import java.util.logging.Level;
import java.util.logging.Logger;
import smartbooks.exceptions.MyException;
import smartbooks.ui.SmartBooksUserSettings;
import smartbooks.ui.helper.OrganizationHelper;
import smartbooks.ui.util.CommonConstants;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.exception.OrganizationException;
import smartbooksdb.exception.UserException;
import smartbooksdb.exception.UserOrganizationMappingException;
import smartbooksdb.hibernatebean.Organization;
import smartbooksdb.hibernatebean.UserOrganizationMapping;
import smartbooksdb.service.OrganizationService;
import smartbooksdb.service.UserOrganizationMappingService;

/**
 *
 * @author Ankit
 */
public class OrganizationManager implements CommonConstants {
    
    /**
     * This method is used to create organization
     * @param organization
     * @return 'true' if the organization is created or else 'false'
     * @throws MyException
     * @throws MandatoryFieldException
     */
    public static boolean createOrganization(Organization organization) throws MyException, MandatoryFieldException {
        try {
            System.out.println(organization.toString());
            OrganizationService organizationService = new OrganizationService();
            
            boolean organizationPanelValidated = OrganizationHelper.validateOrganization(organization);

            if(!organizationService.isOrganizationNameAvailable(organization.getOrgName(), true)) {
                throw new MyException(ORGANIZATION_NAME, "Organization name unavailable.");
            }
            
            if(organizationPanelValidated) {
                Long orgId = organizationService.createOrganization(organization);
                
                if(SmartBooksUserSettings.DEFAULT_ORG_ID==0) {
                    UserManager.setDefaultOrgId(SmartBooksUserSettings.USERNAME, orgId);
                    SmartBooksUserSettings.DEFAULT_ORG_ID = orgId;
                    SmartBooksUserSettings.DEFAULT_ORG_NAME = organization.getOrgName();
                }
                
                if(SmartBooksUserSettings.USER_ID!=0) {
                    UserOrganizationMapping uom = new UserOrganizationMapping();
                    uom.setUserId(SmartBooksUserSettings.USER_ID);
                    uom.setOrgId(orgId);
                    uom.setUpdatedBy(SmartBooksUserSettings.USERNAME);
                    UserOrganizationMappingService uomService = new UserOrganizationMappingService();
                    uomService.createUserOrganizationMapping(uom);
                }
                
                return true;
            }
        } catch (UserOrganizationMappingException | UserException | OrganizationException ex) {
            Logger.getLogger(OrganizationManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to update item
     * @param organization
     * @param editOrganization
     * @return 'true' if the organization is updated or else 'false'
     * @throws MyException
     * @throws MandatoryFieldException
     */
    public static boolean updateOrganization(Organization organization, Organization editOrganization) throws MyException, MandatoryFieldException {
        try {
            System.out.println(organization.toString());
            OrganizationService organizationService = new OrganizationService();
            
            boolean organizationPanelValidated = OrganizationHelper.validateOrganization(organization);

            if(!organization.getOrgName().equals(editOrganization.getOrgName())) {
                if(!organizationService.isOrganizationNameAvailable(organization.getOrgName(), true)) {
                    throw new MyException(ORGANIZATION_NAME, "Organization name unavailable.");
                }
            }
            
            if(organizationPanelValidated) {
                organizationService.updateOrganization(organization);
                
                return true;
            }
        } catch (OrganizationException ex) {
            Logger.getLogger(OrganizationManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
}